//
//  HEIndexViewController.m
//  huien
//
//  Created by yiban on 2017/5/26.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "HEIndexViewController.h"
#import "BannerTableViewCell.h"
#import "InvestTableViewCell.h"
#import "Module2TableViewCell.h"
#import "Html5ViewController.h"
@interface HEIndexViewController ()<UITableViewDataSource,UITableViewDelegate,LcbAutomaticScrollV1Delegate>
{
    
}
@property(nonatomic,strong)LcbUITableView *lcbTableView;
@property(nonatomic,strong)Home2Model *homeModel;
@property(nonatomic,strong)UIView *statusBarView;
@property(nonatomic,assign)CGFloat oldY;
@end

@implementation HEIndexViewController
@synthesize lcbTableView=_lcbTableView;
@synthesize homeModel=_homeModel;
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //
    self.navTitle = @"首页";
    self.headerView.hidden=YES;
    [self initTableView];
   
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(UIView*)statusBarView
{
    if (!_statusBarView) {
        _statusBarView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 20)];
        _statusBarView.backgroundColor = UIColorFromRGB(0x4d8fcb);
        [self.view addSubview:_statusBarView];
    }
    return _statusBarView;
}
-(void)dealloc
{
    if (_lcbTableView) {
        [_lcbTableView removeFromSuperview];
        _lcbTableView=nil;
    }
    [LcbLog log:@"HEIndexViewController--------->dealloc"];
}
#pragma mark - tableview delegate
-(void)initTableView
{
    if (_lcbTableView) {
        [_lcbTableView removeFromSuperview];
        _lcbTableView=nil;
    }
    _lcbTableView =[[LcbUITableView alloc] initWithFrame:CGRectMake(0, -20, SCREEN_WIDTH, SCREEN_HEIGHT-30) style:UITableViewStyleGrouped];
    _lcbTableView.dataSource=self;
    _lcbTableView.delegate=self;
    _lcbTableView.backgroundColor=UIColorHex(0xf3f3f3);
    _lcbTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_lcbTableView];
    __weak typeof(self) weakSelf = self;
    self.lcbTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        //Call this Block When enter the refresh status automatically
        [weakSelf reloadDataa];
    }];
    [self.lcbTableView.mj_header beginRefreshing];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSMutableArray *investcomments =self.homeModel.investments;
    if (investcomments.count==0) {
        return 2;
    }
    return investcomments.count+2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section>1) {
        return 10;
    }
    return 0.01;
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01f;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        return [BannerTableViewCell returnCellHeight];
    }else if (indexPath.section==1)
    {
        return [Module2TableViewCell returnCellHeight];
    }else{
        NSMutableArray *investcomments =self.homeModel.investments;
        if (indexPath.section-2<[investcomments count]) {
            InvestmentsModel *comments = [investcomments objectAtIndex:indexPath.section-2];
            return [InvestTableViewCell returnCellHeight:comments];
        }
        
    }
    return LcbHEIGHT(40);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section==0) {
        BannerTableViewCell  *bannercell = [self.lcbTableView dequeueReusableCellWithIdentifier:@"BannerTableViewCell"];
        if (bannercell == nil) {
            bannercell = [[BannerTableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BannerTableViewCell"];
            bannercell.bannerView.delegate = self;
            bannercell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [bannercell showInfo:self.homeModel indexPath:indexPath];
        return bannercell;
    }else if (indexPath.section==1)
    {
        Module2TableViewCell  *modulecell = [self.lcbTableView dequeueReusableCellWithIdentifier:@"Module2TableViewCell"];
        if (modulecell == nil) {
            modulecell = [[Module2TableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Module2TableViewCell"];
            modulecell.selectionStyle = UITableViewCellSelectionStyleNone;
            __weak typeof(self) weakSelf = self;
            [modulecell setCallBack:^(BEventType type, UIView *v, NSInteger index, id obj, NSIndexPath *path) {
                if (type == BEventType_Module) {
                    ModuleModel *mmodel = nil;
                    for (ModuleModel *mudlemodel in weakSelf.homeModel.moudle) {
                        if (mudlemodel.Id ==index) {
                            mmodel = mudlemodel;
                            break;
                        }
                    }
                    if (mmodel==nil) {
                        return;
                    }
                    [[NativeRouter sharedNativeRouter] nativeRouter:weakSelf params:mmodel completion:^(id obj) {
                        
                    }];
                }
            }];
        }
        [modulecell showInfo:self.homeModel.moudle indexPath:indexPath];
        return modulecell;
    }else{
        InvestTableViewCell  *newscell = [self.lcbTableView dequeueReusableCellWithIdentifier:@"NewsTableViewCell"];
        if (newscell == nil) {
            newscell = [[InvestTableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsTableViewCell"];
            newscell.selectionStyle = UITableViewCellSelectionStyleNone;
            __weak typeof(self) weakSelf = self;
            [newscell setCallBack:^(BEventType type, UIView *v, NSInteger index, id obj, NSIndexPath *path) {
                if (type == BEventType_Module) {
                    InvestmentsModel *comments =(InvestmentsModel *)obj;
                    if (comments.operation.count<2) {
                        return ;
                    }
                    InvestmentsOperationModel *operate =[comments.operation objectAtIndexCheck:1];
                    if (operate) {
                        [[NativeRouter sharedNativeRouter] nativeRouter:weakSelf params:operate completion:^(id obj) {
                            
                        }];
                    }
                    
                }
            }];
        }
        NSMutableArray *investcomments =self.homeModel.investments;
        InvestmentsModel *comments = [investcomments objectAtIndex:indexPath.section-2];
        [newscell showInfo:comments indexPath:indexPath];
        return newscell;
    }
    UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil) {
        cell = [[UITableViewCell  alloc]initWithStyle:UITableViewCellStyleDefault   reuseIdentifier:@"UITableViewCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section>=2) {
         NSMutableArray *investcomments =self.homeModel.investments;
         InvestmentsModel *comments = [investcomments objectAtIndex:indexPath.section-2];
        if (comments.operation.count<1) {
            return ;
        }
        InvestmentsOperationModel *operate =[comments.operation objectAtIndexCheck:0];
        if (operate) {
            [[NativeRouter sharedNativeRouter] nativeRouter:self params:operate completion:^(id obj) {
                
            }];
        }
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.oldY = scrollView.contentOffset.y;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if( scrollView.contentOffset.y > self.oldY) {
        
        if (self.statusBarView.hidden==YES) {
            self.statusBarView.hidden=NO;
        }
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.lcbTableView.contentOffset.y <= 20)
    {
        if (self.statusBarView.hidden==NO) {
            self.statusBarView.hidden=YES;
        }
    }
}

#pragma mark - AutomaticScrollDelegate 点击banner
- (void)tapScrollView:(NSInteger)currentIndexImage {
    
    if (currentIndexImage>=self.homeModel.banners.count) {
        return;
    }
    BannerModel *bannermodel = [self.homeModel.banners objectAtIndex:currentIndexImage];
    [[NativeRouter sharedNativeRouter] nativeRouter:self params:bannermodel completion:^(id obj) {
        
    }];
}
-(void)reloadDataa
{
    __weak typeof(self) weakSelf = self;
    [HEApi appHomeWithParam:nil serviceblock:^(id response, NSString *errorMsg) {
        // 结束刷新
        [weakSelf.lcbTableView.mj_header endRefreshing];
        if ([response isKindOfClass:[LcbBaseModel class]]) {
            LcbBaseModel *baseModel = (LcbBaseModel*)response;
            [[NativeRouter sharedNativeRouter] codeHander:weakSelf params:baseModel completion:nil];
        }else if ([response isKindOfClass:[Home2Model class]]) {
            Home2Model *temModel =(Home2Model*)response;
            weakSelf.homeModel = temModel;
            [weakSelf.lcbTableView reloadData];
        }
       
    }];
}
@end
